package br.com.sovi.gwt.client.rastreamento;


import br.com.sovi.gwt.client.entity.Carro;

import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.control.SmallMapControl;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.user.client.Timer;
import com.gwtext.client.core.Function;
import com.gwtext.client.widgets.Tool;
import com.gwtext.client.widgets.portal.Portlet;

public class CarroMapPortlet extends Portlet {

	private Carro carro;

	public CarroMapPortlet(Carro carro) {
		this.setCarro(carro);
		this.setTitle(getPortletId(carro));
		this.setSize(300, 200);
		this.setCollapsible(true);

		final LatLng latLng = LatLng.newInstance(carro.getPoint()
				.getLatitude(), carro.getPoint().getLongitude());
		
		final MapWidget map = new MapWidget();
		map.setSize("100%", "100%");
		map.setCenter(latLng, 10);
		map.addControl(new SmallMapControl());
		map.addOverlay(new Marker(latLng));

		this.add(map);
		
		this.addTool(new Tool(Tool.RESTORE, new Function() {
			@Override
			public void execute() {
				map.panTo(latLng);
			}
		}));

		new Timer() {
			@Override
			public void run() {
				map.checkResizeAndCenter();
			}
		}.schedule(1);
	}

	public static String getPortletId(final Carro carro) {
		return carro.getMarca() + " - " + carro.getModelo();
	}

	public void setCarro(Carro carro) {
		this.carro = carro;
	}

	public Carro getCarro() {
		return carro;
	}

}
